//#include "udp_includes.h"
#include "../includes.h"


_st_server_download DLc={0,0,0,0 ,0,0,0,0};

int fdWR = -1;

int task_client_Download()
{
	int mod;
	_st_server_download *p=&DLc;

	//if( stDF.first ){ memset(&stDF,0,sizeof(stDF); stDF.blocksize = 512; }

	if( p->Step == dns_nothing )return 0;
	p->iElapsed++; p->iSubElapsed++;
	if( p->iElapsed > M_UDP_TASK_TIME_OUT
		||	p->iTry > M_UDP_TASK_MAX_RETRY_CNT
	){
		p->Step = dns_nothing;
		memset(&DLc,0,sizeof(_st_server_download));
	}
	//dl_printf("client>> %d,%5d) %5d,%5d",p->Step,p->iSubElapsed, p->iElapsed,p->iTry);
	return 0;
}



LOCAL_CMD_DATA_ lcd_data={0,0,0,0	,M_LOCAL_CMD_DATA_SIZE, };

int msg_client_respond_req(char *ptr,int cmd,int len)
{
	IPC_Data_T *pIn;
	KTI_SubData_CtrlReq_T* 	p1 = (KTI_SubData_CtrlReq_T* )ptr;
	KTI_SubData_IpReq_T* 	p2 = (KTI_SubData_IpReq_T* 	 )ptr;
	KTI_SubData_HeartbeatReq_T* 	p3 = (KTI_SubData_HeartbeatReq_T* 	 )ptr;
	pIn = &udp.msg.in;

	int i;

	switch(cmd){
		case KTC_ALARM_REPORT:
			udp_printf(">> rcv>>KTC_ALARM_REPORT\n");
			bAlarmFalg = 0;
			break;
		case KTC_CONTRL_REQ:
			udp_printf(">> KTC_CONTRL_RSP:%d, %d\n",p1->event,p1->period);
			if( p1->event )
			{
				if( CONFIG.iPeriodUdpReport != p1->period ){
					if( frc_error.inore_range ||
							( CheckWithinTheConfigRange(p1->period, cStatusReportPeriod ) ) // value,index
					){
						CONFIG.iPeriodUdpReport = p1->period;
						buf_report.period = p1->period;
						CONFIG.WriteEvent = ON;
						
						iCntPeriodUdpReport = M_DEFINE_START_VALUE+1;
					}
				}
				//iCntPeriodUdpReport = M_DEFINE_START_VALUE;
				msg_pack_data_report2nd(&udp,pIn->ipc.sequence,5);
			}
			else msg_pack_data_report2nd(&udp,-1 ,5);
			break;

		case KTC_IP_CHANGE_REQ:
			udp_printf(">> KTC_IP_CHANGE_REQ: %d.%d.%d.%d:%d \n",p2->ip[0],p2->ip[1],p2->ip[2],p2->ip[3],p2->port);
			//sprintf( str_ip_server,"%d.%d.%d.%d",(p2->ip[0])&0xFF ,(p2->ip[1])&0xFF ,(p2->ip[2])&0xFF ,(p2->ip[3])&0xFF );
			//port_server = p2->port ;
			/*
			 */
			sprintf(sicf.v[M_IP_CFG_Remote_IP_INDEX],"%d.%d.%d.%d",p2->ip[0] ,p2->ip[1] ,p2->ip[2],p2->ip[3] );
			sprintf(sicf.v[M_IP_CFG_Remote_PORT_INDEX],"%d",p2->port );
			net_close(&udp);
			net_open(&udp);
			write_user_cfg();
			msg_pack_ip_rsp(&udp,pIn->ipc.sequence);

			break;

		case KTC_RESET_REQ:
			iCntResetFlag = 3;
			udp_printf(">> KTC_RESET_REQ\n");
			msg_pack_reset_rsp(&udp,pIn->ipc.sequence);
			write_rst_info("Reset");
			break;

		case KTC_STATUS_REQ:
			udp_printf(">>KTC_STATUS_REQ\n");
			msg_pack_data_report2nd(&udp,pIn->ipc.sequence,1);
			break;

		case KTC_MAC_ADDR_REQ:
			udp_printf(">>KTC_MAC_ADDR_REQ\n");
			msg_pack_mac_rsp(&udp,pIn->ipc.sequence);
			break;

		case KTC_LOCAL_REQ:
			//len = min(pIn->ipc.len,M_LOCAL_CMD_DATA_SIZE);
			for(i=0; i<len ; i++){
				lcd_data.buf[ lcd_data.head++] = ptr[i];
				if( lcd_data.head >= lcd_data.size ) lcd_data.head = 0;
			}
			udp_printf(">>KTC_LOCAL_REQ:%d(%d,%d),%c\n"
					,len ,lcd_data.head ,lcd_data.tail ,ptr[0] );
			//msg_pack_CmdRsp(&udp,-1,"KTC_LOCAL_REQ",13);
			break;
		case KTC_HEARTBEAT_REQ:
			if( p3->flag ){
				if( CONFIG.iPeriodHeartBeat != p3->period ){
					if( frc_error.inore_range ||
							( CheckWithinTheConfigRange(p3->period, cHeartBeatPeriod ) ) // value,index
					){
						CONFIG.iPeriodHeartBeat = p3->period;
						CONFIG.WriteEvent = ON;
						
						iCntPeriodHeartBeat = M_DEFINE_START_VALUE+1;
					}
				}
			}
			msg_pack_data_heartbeat_rsp(&udp,pIn->ipc.sequence);
			break;
	}
	return 0;
}

KTI_SubData_DL_CmdRsp_T stDLrsp;
KTI_SubData_DL_DataRsp_T stDLDataRsp;


int file_size_downloaded ;
int iTimeOutDownload=0;
int msg_client_respond_download(char *ptr,int cmd,int len)
{
	IPC_Data_T *pIn;
	pIn = &udp.msg.in;

	KTI_SubData_DL_CmdReq_T *p=(KTI_SubData_DL_CmdReq_T *)ptr;
	KTI_SubData_DL_Data_T 	*p1=(KTI_SubData_DL_Data_T *)ptr;
	KTI_SubData_DL_CompleteReq_T *p2= (KTI_SubData_DL_CompleteReq_T *)ptr;

	KTI_SubData_DL_CmdRsp_T *pp=&stDLrsp;
	KTI_SubData_DL_DataRsp_T *pp1=&stDLDataRsp;
	switch( cmd )
	{
		case KTC_DL_CMD_REQ:
			pp->supplier1 = p->supplier1;
			pp->supplier2 = p->supplier2;
			pp->total_frame = p->total_frame;
			pp->version[0] = p->version[0];
			pp->version[1] = p->version[1];
			if( fdWR > 0 )	close(fdWR);
#ifndef WIN_APP
			unlink(M_DL_FILE_NAME_WR);
#else
			DeleteFile(M_DL_FILE_NAME_WR); //
#endif
			//fdWR = open(M_DL_FILE_NAME_WR, O_WRONLY | O_CREAT | O_BINARY | O_APPEND );
			if( pp->supplier1 == 0x50 && pp->supplier2 == 0x50  ){
				fdWR = open(M_DL_FILE_NAME_WR, O_WRONLY | O_CREAT | O_APPEND );
				if( fdWR > 0 )	pp->ready = 1;
				else{
					pp->ready = 0;
					//break; // on error
				}
			}
			else { pp->ready = 0; }
			pp->rsvd = 0x80;
			udp_printf("DN ReqInfo>>>> "
						"s:0x%02X 0x%02X > %d"
						"v:0x%02X 0x%02X"
						"\r\n"
						,p->supplier1&0xFF, p->supplier2&0xFF, p->total_frame&0xFFFF
						,p->version[0]&0xFF,p->version[1]&0xFF
			);
			if( pp->ready ){udp_printf(">>DN Ready:: %d ",pp->ready); }
			else 		   {udp_printf("******DN Ready:: %d ",pp->ready);}
			msg_pack_download_rsp(&udp,pIn->ipc.sequence,pp);
			file_size_downloaded = 0;

			break;

		case KTC_DL_DATA:
			//pp1->current_frame = ((p1->current_frame == 0)?0xFFFF:p1->current_frame);
			pp1->current_frame = p1->current_frame;
			pp1->total_frame   = p1->total_frame;
			write(fdWR,(void*)((int)ptr+4),len-4);
			file_size_downloaded += ( len-4);
			iTimeOutDownload = M_TimeOutDownload;
			msg_pack_download_data_rsp(&udp,pIn->ipc.sequence, pp1);
			break;

		case KTC_DL_COMPLETE_REQ:
			close(fdWR);
			fdWR = -1;
			udp_printf("total filesize:%d >>>>>>>>>>>\n",file_size_downloaded);
			total_crc_saved = p2->total_crc;
			iFWUpdate = 11;
			break;
	}
	return 0;
}

char *GetUdpCmd()
{
	char *p = NULL;

	return p;
}
